<template>
  <div class="question-container">
    <el-card>
      <el-row>
        <el-col :span="6">
          <el-form label-width="80px" size="small">
            <el-form-item label="关键字">
              <el-input
                placeholder="根据编号搜索"
                v-model="pageParams.keyword"
              ></el-input>
            </el-form-item>
          </el-form>
        </el-col>
        <el-col :span="6" :offset="12" style="text-align: right">
          <el-button size="small" @click="pageParams.keyword = ''"
            >清除</el-button
          >
          <el-button type="primary" size="small" @click="loadRandoms()"
            >搜索</el-button
          >
        </el-col>
      </el-row>
      <el-row>
        <el-alert :title="'数据一共 ' + total + ' 条'" type="info" show-icon>
        </el-alert>
      </el-row>
      <el-row>
        <el-table :data="randomsList" fit>
          <el-table-column prop="id" label="编号" width="220px">
          </el-table-column>
          <el-table-column label="题型" width="80px">
            <template v-slot="scope">
              {{ queType[scope.row.questionType] }}
            </template>
          </el-table-column>
          <el-table-column label="题目编号" width="220px">
            <template v-slot="scope">
              <el-link
                v-for="(item, index) in scope.row.questionIDs"
                :key="index"
              >
                {{ item.number }}
              </el-link>
            </template>
          </el-table-column>
          <el-table-column prop="addTime" label="录入时间" width="180px">
          </el-table-column>
          <el-table-column prop="totalSeconds" label="答题时间(s)">
          </el-table-column>
          <el-table-column prop="accuracyRate" label="正确率(%)">
          </el-table-column>
          <el-table-column prop="userName" label="录入人"> </el-table-column>
          <el-table-column label="操作">
            <template v-slot="scope">
              <el-button
                type="danger"
                icon="el-icon-delete"
                circle
                plain
                @click="hDel(scope.row)"
              ></el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-row>
      <el-row style="text-align: right">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page.sync="pageParams.page"
          :page-sizes="[5, 10, 20, 50]"
          :page-size="pageParams.pagesize"
          layout="prev, pager, next, sizes, jumper"
          :total="total"
        >
        </el-pagination
      ></el-row>
    </el-card>
  </div>
</template>

<script>
import { randoms, removeRandoms } from "@/api/hmmm/questions";
import { questionType } from "@/api/hmmm/constants";

export default {
  data() {
    return {
      pageParams: {
        page: 1,
        pagesize: 10,
        keyword: "",
      },
      total: 0,
      randomsList: [],
    };
  },
  computed: {
    queType() {
      return questionType.reduce((sum, item) => {
        sum[item.value] = item.label;
        return sum;
      }, {});
    },
  },
  created() {
    this.loadRandoms();
  },
  methods: {
    async loadRandoms() {
      const res = await randoms(this.pageParams);
      console.log(res);
      this.randomsList = [...res.data.items];
      this.total = res.data.counts;
    },
    handleSizeChange(curSize) {
      this.pageParams.pagesize = curSize;
      this.loadRandoms();
    },
    handleCurrentChange(curPage) {
      this.pageParams.page = curPage;
      this.loadRandoms();
    },
    hDel(data) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          console.log(data);
          this.doDel(data);
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    async doDel(data) {
      try {
        const res = await removeRandoms(data);
        console.log(res);
        this.$message.success("删除成功");
        if (this.total % this.pageParams.pagesize === 1) {
          this.pageParams.page--;
          this.pageParams.page = Math.max(1, this.pageParams.page);
        }
        this.loadRandoms();
      } catch (err) {
        this.$message.error(err.message);
      }
    },
  },
};
</script>

<style scoped lang='less'></style>
